//
//  DCDatePickerView.m
//  Demo
//
//  Created by zhaolin on 2023/1/30.
//

#import "DCDatePickerView.h"

@interface DCDatePickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic, strong) UIPickerView *datePicker;

@property (nonatomic, strong) NSMutableArray *yearArr;

@property (nonatomic, strong) NSMutableArray *monthArr;

@property (nonatomic, strong) NSMutableArray *dayArr;

@property (nonatomic, assign) NSInteger currentYearIndex;
@property (nonatomic, assign) NSInteger currentMonthIndex;
@property (nonatomic, assign) NSInteger currentDayIndex;


@end

@implementation DCDatePickerView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        [self configureAlloc];
        [self configreUI];
    }
    return self;
}

- (void)configureAlloc {
    _yearArr = @[].mutableCopy;
    _monthArr = @[].mutableCopy;
    _dayArr = @[].mutableCopy;
    NSDate * currentDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    NSInteger currentYear = [calendar component:NSCalendarUnitYear fromDate:currentDate];
    NSInteger currentMonth = [calendar component:NSCalendarUnitMonth fromDate:currentDate];
    NSInteger currentDay = [calendar component:NSCalendarUnitDay fromDate:currentDate];
    
    int yearLenth = 2;
    NSInteger startYear = currentYear - yearLenth/2;
    for (int i=0; i < yearLenth; i++) {
        [_yearArr addObject:[NSString stringWithFormat:@"%ld",startYear]];
        if(currentYear == startYear) {
            _currentYearIndex = i;
        }
        startYear++;
    }
    for (int i = 1; i <= 12; i++) {
        [_monthArr addObject:[NSString stringWithFormat:@"%02d",i]];
        if(currentMonth == i) {
            _currentMonthIndex = i-1;
        }
    }
     
    NSArray * daysOfMouth = [self getDaysOfYear:[NSString stringWithFormat:@"%ld",currentYear] Mouth:[NSString stringWithFormat:@"%2ld",currentMonth]];
    [_dayArr addObjectsFromArray:daysOfMouth];
    for (int i=1; i<=_dayArr.count; i++) {
        if(currentDay == i) {
            _currentDayIndex = i-1;
        }
    }
    
}

- (NSArray *)getDaysOfYear:(NSString *)year Mouth:(NSString *)mounth {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM";
    NSString *dateStr = [NSString stringWithFormat:@"%@-%@",year,mounth];
    NSDate *targartDate = [formatter dateFromString:dateStr];
    NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:targartDate];
    NSInteger daysOfMouth = range.length;
    NSMutableArray *daysArr = @[].mutableCopy;
    for (int i=1; i <=daysOfMouth; i++) {
        [daysArr addObject:[NSString stringWithFormat:@"%d",i]];
    }
    return daysArr;
}

- (void)configreUI {
//    self.backgroundColor = [[UIColor colorWithString:@"#000000"] colorWithAlphaComponent:0.7];
    UIView*bgView = [UIView new];
    bgView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(342);
    }];
    
    UIView *titleView = [UIView new];
    titleView.backgroundColor = [UIColor colorWithString:@"#F0FFCE"];
    [bgView addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(53);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"Date of Birth";
    titleLabel.textColor = [UIColor colorWithString:@"#1C221B"];
    titleLabel.font = DCFont_PF_Semibold(14);
    [titleView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(titleView);
    }];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.layer.borderWidth = 1;
    cancelBtn.layer.borderColor = [UIColor colorWithString:@"#CED7CD"].CGColor;
    cancelBtn.layer.masksToBounds = YES;
    cancelBtn.layer.cornerRadius = 4;
    cancelBtn.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    cancelBtn.titleLabel.font = DCFont_PF_Regular(14);
    [cancelBtn setTitleColor:[UIColor colorWithString:@"#CED7CD"] forState:UIControlStateNormal];
    [cancelBtn setTitle:@"CANCEL" forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(53);
        make.right.mas_equalTo(bgView.mas_centerX).mas_offset(-10);
        make.height.mas_equalTo(42);
        make.bottom.mas_equalTo(-49);
    }];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    sureBtn.layer.borderWidth = 1;
//    sureBtn.layer.borderColor = [UIColor colorWithString:@"#CED7CD"].CGColor;
    sureBtn.layer.masksToBounds = YES;
    sureBtn.layer.cornerRadius = 4;
    sureBtn.backgroundColor = [UIColor colorWithString:@"#000000"];
    sureBtn.titleLabel.font = DCFont_PF_Medium(14);
    [sureBtn setTitleColor:[UIColor colorWithString:@"#FFFFFF"] forState:UIControlStateNormal];
    [sureBtn setTitle:@"CONFIRM" forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-53);
        make.left.mas_equalTo(bgView.mas_centerX).mas_offset(10);
        make.height.mas_equalTo(42);
        make.bottom.mas_equalTo(-49);
    }];
    [bgView addSubview:sureBtn];
    
    
    _datePicker = [[UIPickerView alloc] init];
    _datePicker.showsSelectionIndicator= NO;
    _datePicker.backgroundColor = [UIColor clearColor];
    _datePicker.tintColor = [UIColor clearColor];
    _datePicker.delegate = self;
    _datePicker.dataSource = self;
    [bgView addSubview:_datePicker];
    [_datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(titleView.mas_bottom);
        make.bottom.mas_equalTo(cancelBtn.mas_top);
    }];
    [self.datePicker selectRow:_currentDayIndex inComponent:0 animated:YES];
    [self.datePicker selectRow:_currentMonthIndex inComponent:1 animated:YES];
    [self.datePicker selectRow:_currentYearIndex inComponent:2 animated:YES];
}

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component==0) {
        return self.dayArr.count;
    } else if(component == 1) {
        return self.monthArr.count;
    } else {
        return self.yearArr.count;
    }
}

//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//    if (component==0) {
//        return [self.yearArr objectAtIndex:component];
//    } else {
//        return [self.monthArr objectAtIndex:component];
//    }
//}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return 80;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 54;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:DCFont_PF_Regular(14)];
        [pickerLabel setTextColor:[UIColor colorWithString:@"#1C221B"]];
        pickerLabel.textAlignment = NSTextAlignmentCenter;
    }
//    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    if (component==0) {
        pickerLabel.text = [self.dayArr objectAtIndex:row];
    } else if (component==1){
        pickerLabel.text = [self.monthArr objectAtIndex:row];
    } else {
        pickerLabel.text = [self.yearArr objectAtIndex:row];
    }
    return pickerLabel;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component  {
    if(component!=0) {
        NSLog(@"%@",pickerView);
    }
}



- (void)cancelAction:(UIButton *)sender {
    if(self.cancelCallBack) {
        self.cancelCallBack(@"");
    }
}

- (void)sureAction:(UIButton *)sender {
    NSString *targatYear = [_yearArr objectAtIndex:[_datePicker selectedRowInComponent:2]];
    NSString *targatMonth = [_monthArr objectAtIndex:[_datePicker selectedRowInComponent:1]];
    NSString *targatDay = [_dayArr objectAtIndex:[_datePicker selectedRowInComponent:0]];
    NSString *date = [NSString stringWithFormat:@"%@-%@-%@",targatYear,targatMonth,targatDay];
    if(self.sureCallBack) {
        self.sureCallBack(date);
    }
    
}

@end
